﻿using System;
using System.Collections.Generic;
using System.Linq;

using System.Web.Mvc;
using Prodma.DataAccess.Database;
using Prodma.DataAccess.Models;
using Prodma.DataAccess.Helper.Enumerators;
using Prodma.DataAccess.Models.ViewModels;
using System.Configuration;

namespace Prodma.DataAccess.DataService.ListService
{
    public static class ContentList
    {

        private static DataContext context = new DataContext();

        #region Show ContentName Menu Item Selected

        public static string GetContentName(int id)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var name = (from entity in context.tb_Content
                            where entity.Id == id
                            select entity).FirstOrDefault();

                return name.Name;
            }
        }
        #endregion

        #region Panel Management

        public static IList<ContentVm> GetLastContentList(int count)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var list = (from content in context.tb_Content
                            join order in context.tb_ContentOrder
                                on content.Id equals order.Content
                            select new ContentVm()
                            {
                                Id = content.Id,
                                Name = content.Name,
                                Category = new CategoryVm() { Id = content.tb_Category.Id, Name = content.tb_Category.Name },
                            }).OrderByDescending(x => x.Id).Take(count).ToList();

                return list;
            }
        }
        #endregion

        #region Menu Select Ajax

        public static IList<ContentVm> GetListForMenu(int page)
        {
            int defaultArchiveId = Convert.ToInt32(ConfigurationManager.AppSettings["defaultArchive"]);

            int limitValue = Convert.ToInt32(ConfigurationManager.AppSettings["popupDataPagerLimit"]);
            int skipValue = page * limitValue;
            DataContext context = new DataContext();
            using (context)
            {
                var list = (from content in context.tb_Content
                            join order in context.tb_ContentOrder
                                on content.Id equals order.Content
                            where content.tb_Archive.Id == defaultArchiveId
                            select new ContentVm()
                            {
                                Id = content.Id,
                                Name = content.Name,
                                Category = new CategoryVm() { Id = content.tb_Category.Id, Name = content.tb_Category.Name },
                                Order = new ContentOrderVm() { OrderNo = order.OrderNo },
                                Language = new LanguageVm() { Id = content.tb_Language.Id, Name = content.tb_Language.Name },
                            }).OrderByDescending(x => x.Order.OrderNo).Skip(skipValue).Take(limitValue).ToList();

                return list;
            }
        }
        public static int GetTotalRowCountForMenu()
        {
            int defaultArchiveId = Convert.ToInt32(ConfigurationManager.AppSettings["defaultArchive"]);
            DataContext context = new DataContext();
            using (context)
            {
                int rowCount = (from content in context.tb_Content
                                where content.tb_Archive.Id == defaultArchiveId
                                select content).Count();

                return rowCount;
            }
        }
        public static IList<ContentVm> GetListForMenu(int page, Dictionary<string, string> find)
        {
            int defaultArchiveId = Convert.ToInt32(ConfigurationManager.AppSettings["defaultArchive"]);

            int limitValue = Convert.ToInt32(ConfigurationManager.AppSettings["popupDataPagerLimit"]);
            int skipValue = page * limitValue;

            string findName = find["name"];
            DataContext context = new DataContext();
            using (context)
            {
                var list = (from content in context.tb_Content
                            join order in context.tb_ContentOrder
                                on content.Id equals order.Content
                            where content.Name.StartsWith(findName) &&
                                  content.tb_Archive.Id == defaultArchiveId
                            select new ContentVm()
                            {
                                Id = content.Id,
                                Name = content.Name,
                                Category = new CategoryVm() { Id = content.tb_Category.Id, Name = content.tb_Category.Name },
                                Order = new ContentOrderVm() { OrderNo = order.OrderNo },
                                Language = new LanguageVm() { Id = content.tb_Language.Id, Name = content.tb_Language.Name },
                            }).OrderByDescending(x => x.Order.OrderNo).Skip(skipValue).Take(limitValue).ToList();

                return list;
            }
        }
        public static int GetTotalRowCountForMenu(Dictionary<string, string> find)
        {
            int defaultArchiveId = Convert.ToInt32(ConfigurationManager.AppSettings["defaultArchive"]);

            string findName = find["name"];
            DataContext context = new DataContext();
            using (context)
            {
                int rowCount = (from content in context.tb_Content
                                where content.Name.StartsWith(findName) &&
                                      content.tb_Archive.Id == defaultArchiveId
                                select content).Count();

                return rowCount;
            }
        }
        public static IList<int> GetCountsForMenu()
        {
            DataContext context = new DataContext();
            using (context)
            {
                var countList = (from content in context.tb_Content
                                 group content by content.tb_Category.Id into category
                                 select category.Count()).ToList();

                return countList;
            }
        }
        public static IList<int> GetCategoryListForMenu()
        {
            DataContext context = new DataContext();
            using (context)
            {
                var list = (from content in context.tb_Content
                            group content by content.tb_Category.Id into category
                            select category.Key).ToList();

                return list;
            }
        }
        #endregion
        
    }
}
